﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using BF.Model;
using BF.IBLL.BaseSystem.AreaManagement;
using Base.Core.UnityFactory;
using System.Text;
using System.Threading;
using System.Xml.Serialization;
using System.Data;
namespace BF.Web.WebService
{
    /// <summary>
    /// AreaService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class AreaService : System.Web.Services.WebService
    {
        #region 接口

        private readonly Isys_AreaBLL iBLL = IOCFactory.GetIOCResolve<Isys_AreaBLL>();

        #endregion 接口

        #region WebService

        /// <summary>
        /// 根据superID获取列表
        /// </summary>
        /// <param name="superID"></param>
        /// <returns>TreeNode集合</returns>
        [WebMethod]
        public List<TreeNode> GetTree(string superID)
        {
            List<TreeNode> list = new List<TreeNode>();
            foreach (DataRow item in GetSysArea(superID).Rows)              //集合转换
            {
                TreeNode node = new TreeNode();
                node.Id = item["sID"].ToString();
                node.Text = item["sName"].ToString();
                bool lafe = Int64.Parse(item["childLen"].ToString()) == 0;
                node.Leaf = lafe;
                if (!lafe)
                {
                    TreeNode c = new TreeNode();
                    c.Text = "test";
                    c.Id = "0";
                    c.Leaf = true;
                    node.Childrens = new List<TreeNode>();
                    node.Childrens.Add(c);
                }

                list.Add(node);
            }
            return list;
        }

        /// <summary>
        /// 测试
        /// </summary>
        /// <param name="rootID"></param>
        /// <param name="areaName"></param>
        /// <returns>AreaCode集合</returns>
        [WebMethod]
        public List<string> SearchTree(string rootID, string areaName)
        {
            //获取该root的详细信息
            sys_Area areaModel = iBLL.GetModelByID(rootID);
            if (areaModel != null)
            {
                string sIDKeyWord;          //为查询关键字添加通配符
                switch (areaModel.iLevel)
                {
                    case 0: sIDKeyWord = "000000"; break;
                    case 1: sIDKeyWord = areaModel.sID.Substring(0, 2); break;
                    case 2: sIDKeyWord = areaModel.sID.Substring(0, 4); break;
                    case 3: sIDKeyWord = areaModel.sID.Substring(0, 6); break;
                    case 4: sIDKeyWord = areaModel.sID.Substring(0, 9); break;
                    case 5: sIDKeyWord = areaModel.sID.Substring(0, 12); break;
                    default: return null;
                }
                //数据查询并转为List<sring>集合(返回值：areaCode集合)
                List<string> areaCodeList = iBLL.GetList(p => (sIDKeyWord == "000000" || p.sID.StartsWith(sIDKeyWord)) && p.sName.Contains(areaName) && p.sID != rootID).OrderBy(p => p.sID).Select(p => p.sID).ToList();
                return areaCodeList;
            }
            return null;
        }
        [WebMethod]
        public List<TreeNode> GetSearchTree(string superID, string areaID)
        {
            Dictionary<int, string> areaPath = new Dictionary<int, string>();
            //获取地区路径集合
            string tempAreaID = areaID;
            while (true)
            {
                sys_Area areaModel = iBLL.GetModelByID(tempAreaID);
                areaPath.Add(areaModel.iLevel, areaModel.sID);
                if (areaModel.sID == superID || areaModel == null)
                {
                    break;
                }
                else
                {
                    tempAreaID = areaModel.sSuperID;
                }
            }


            TreeNode treeNode = new TreeNode();
            TreeNode superNode = new TreeNode();
            string tempSuperID = superID;
            bool isFirst = true;
            while (true)
            {
                if (tempSuperID == areaID) { break; }
                //创建子节点
                List<TreeNode> childrenList = new List<TreeNode>();
                foreach (DataRow item in GetSysArea(tempSuperID).Rows)              //集合转换
                {
                    TreeNode node = new TreeNode();
                    node.Id = item["sID"].ToString();
                    node.Text = item["sName"].ToString();
                    bool lafe = Int64.Parse(item["childLen"].ToString()) == 0;
                    node.Leaf = lafe;
                    if (!lafe)
                    {
                        TreeNode c = new TreeNode();
                        c.Text = "test";
                        c.Id = "0";
                        c.Leaf = true;
                        node.Childrens = new List<TreeNode>();
                        node.Childrens.Add(c);
                    }
                    childrenList.Add(node);
                }

                TreeNode[] tempArray = new TreeNode[childrenList.Count];
                childrenList.CopyTo(tempArray);
                if (isFirst)
                {
                    treeNode.Childrens = tempArray.ToList();
                    isFirst = false;
                }
                else
                {
                    superNode.Childrens = tempArray.ToList();
                }

                superNode = tempArray.FirstOrDefault(p => areaPath.Values.Contains(p.Id));
                if (superNode == null || superNode.Leaf)
                {
                    break;
                }
                tempSuperID = superNode.Id;
            }

            //return treeNode.Childrens.FirstOrDefault(p => areaPath.Values.Contains(p.Id)).Childrens;
            return treeNode.Childrens;
        }

        #endregion WebService

        #region 自定义方法

        /// <summary>
        /// 根据上级地区编码获取地区数据
        /// </summary>
        /// <param name="superID">上级地区编码</param>
        /// <returns>查询结果</returns>
        protected DataTable GetSysArea(string superID)
        {
            return BF.Common.SqlHelper.ExecuteStoredProcedure("GetAreaList", "WSJD_BaseFrameworkEntities", new System.Data.SqlClient.SqlParameter("superID", superID));
        }



        #endregion 自定义方法
    }


    #region 自定义类型
    public class TreeNode
    {
        [XmlAttribute("id")]
        public string Id { get; set; }
        [XmlAttribute("text")]
        public string Text { get; set; }
        [XmlAttribute("leaf")]
        public bool Leaf { get; set; }
        [XmlElement("Childrens")]
        public List<TreeNode> Childrens { get; set; }
    }

    public class children
    {
        [XmlAttribute("id")]
        public string id { get; set; }
        [XmlAttribute("text")]
        public string text { get; set; }
        [XmlAttribute("leaf")]
        public bool leaf { get; set; }
        [XmlAttribute("hidden")]
        public bool hidden { get; set; }
    }

    #endregion 自定义类型
}
